//
//  SportsShoesViewController.m
//  B
//
//  Created by lanou on 16/5/5.
//  Copyright © 2016年 裴启航. All rights reserved.
//


#define kShoesURL @"http://app.dunkhome.com/v2/news_v3?c=155&slide=1"
@interface SportsShoesViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
// 存放所有的数据
@property (nonatomic, strong) NSMutableArray *dataSourceArr;
@property (nonatomic, strong)UICollectionView *collection;
// 轮播图数组
@property (nonatomic, strong) NSMutableArray *imagesArr;

@end

@implementation SportsShoesViewController
// 懒加载
- (NSMutableArray *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [[NSMutableArray alloc] init];
    }
    return _dataSourceArr;
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO; //  固定ScrollView
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:kShoesURL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *shoesArr = responseObject[@"table_view_data"];
        for (NSDictionary *dic in shoesArr) {
            SportShoesModel *myModel = [[SportShoesModel alloc] init];
            [myModel setValuesForKeysWithDictionary:dic];
            [self.dataSourceArr addObject:myModel];
        }
        // 创建collectiView
        if (self.dataSourceArr.count) {
            [self createCollection];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collection reloadData];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败===%@", error);
    }];
    
    NSMutableArray *imagesArr = [@[]mutableCopy];
    for (int i = 1; i < 4; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"medium%d.jpg", i]];
        [imagesArr addObject:image];
    }
    CycleScrollVIew *cycleSV = [[CycleScrollVIew alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 150) imagesArray:imagesArr animationDuration:2];
    [self.view addSubview:cycleSV];
}
- (void)createCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((ScreenWidth - 20) / 2, 200);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0 + 150 + 20, ScreenWidth, ScreenHeight - 64 - 150 - 20) collectionViewLayout:layout];
    
    collection.backgroundColor = [UIColor whiteColor];
    collection.delegate = self;
    collection.dataSource = self;
    
    [collection registerNib:[UINib nibWithNibName:@"SportShoesCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([SportShoesModel class])];
    self.collection = collection;
    [self.view addSubview:collection];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SportShoesModel *model = [self.dataSourceArr objectAtIndex:indexPath.row];
    BaseCollectionViewCell *cell = [CollectionViewCellFactory cellWithModelClass:[model class] collectinView:collectionView index:indexPath];
    [cell setDataWithModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SportShoesModel *mode = [self.dataSourceArr objectAtIndex:indexPath.row];
    ShoesDetailViewController *detail = [ShoesDetailViewController new];
    BaseNavigationController *navi = self.view.window.rootViewController.childViewControllers[1];
    detail.ShoesID = mode.ID.stringValue;
    [navi pushViewController:detail animated:YES];
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
